package com.rbac.mapper.test;

import com.rbac.pojo.Permission;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface PermissionMapper2 {

    @Insert("insert into permission (name, description) values (#{name}, #{description})")
    void save(Permission permission);

    @Select("select * from permission where id in " +
            "(select permission_id from role_permission where role_id in " +
            "(select role_id from user_role where user_id = #{userId}))")
    List<Permission> getList(Integer userId);

    @Select("select * from permission")
    List<Permission> getListAll();

    @Select("select * from permission where id in " +
            "(select permission_id from role_permission where role_id = #{roleId}) and level = 1")
    List<Permission> getListByRoleId(Integer roleId);

    @Delete("delete from role_permission where role_id = #{roleId}")
    void deleteRolePermission(Integer roleId);

    @Insert("insert into role_permission (role_id, permission_id) values (#{roleId}, #{permissionId})")
    void addRolePermission(Integer roleId, Integer permissionId);
}
